﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace MarkupDesignForm
{
    class gethtml
    {
        private del_runaction v_del_runaction;
        public delegate void del_ev_paginacargada(string texto, bool ismain, bool nuevav);
        public event del_ev_paginacargada ev_paginacarga;
        getxml new_xml;
        f_obteniendo loadi;
        struct str_param
        {
            public string url;
            public string param;
            public string metodo;
            public bool is_main;
            public bool nv;
        }
        public gethtml()
        {
            ev_paginacarga += new del_ev_paginacargada(gethtml_ev_paginacarga);
            loadi = new f_obteniendo();
        }
        public void llamarpagina(del_runaction vaction)
        {
            //ev_paginacarga += new del_ev_paginacargada(gethtml_ev_paginacarga);
            v_del_runaction = vaction;
        }
        void gethtml_ev_paginacarga(string texto,bool ismain,bool nuevav)
        {
            //throw new NotImplementedException();
            //texto = texto.Replace("\r\n", "");
            //texto = texto.Replace("\t", "");
            loadi.Hide();
            if (ismain)
            {
                Application.Run(new Form1(texto));
                //new_xml = new getxml();
                //new_xml.get_xml(texto);
            }
            else if (nuevav)
            {
                Application.Run(new Form1(texto));
            }
            else
                v_del_runaction(texto);

        }
        public void asyn_cargarpagina(string url, string parametros, string metodo,bool main, bool nuevav)
        {
            loadi.Show();
            str_param p;
            p.url = url;
            p.param = parametros;
            p.metodo = metodo;
            p.is_main = main;
            p.nv = nuevav;
            Thread ptr = new Thread(new ParameterizedThreadStart(HttpPost));
            ptr.Start(p);
        }
        void HttpPost (object p)
        {
            str_param pat = (str_param)p;
            string devolver = "";
           // parameters: name1=value1&name2=value2	
            WebRequest webRequest = WebRequest.Create(pat.url);
           //string ProxyString = 
           //   System.Configuration.ConfigurationManager.AppSettings
           //   [GetConfigKey("proxy")];
           //webRequest.Proxy = new WebProxy (ProxyString, true);
           //Commenting out above required change to App.Config
           webRequest.ContentType = "application/x-www-form-urlencoded";
           webRequest.Method = pat.metodo.ToUpper();
           byte[] bytes = Encoding.ASCII.GetBytes(pat.param);
           Stream os = null;
           try
           { // send the Post
              webRequest.ContentLength = bytes.Length;   //Count bytes to send
              os = webRequest.GetRequestStream();
              os.Write (bytes, 0, bytes.Length);         //Send it
           }
           catch (WebException ex)
           {
              MessageBox.Show ( ex.Message, "HttpPost: Request error", 
                 MessageBoxButtons.OK, MessageBoxIcon.Error );
              loadi.Hide();
           }
           finally
           {
              if (os != null)
              {
                 os.Close();
              }
           }
         
           try
           { // get the response
              WebResponse webResponse = webRequest.GetResponse();
              if (webResponse == null) 
                 { devolver = "null";
                 ev_paginacarga(devolver, pat.is_main,pat.nv);
              }
              StreamReader sr = new StreamReader (webResponse.GetResponseStream());
              devolver = sr.ReadToEnd ().Trim ();
              ev_paginacarga(devolver, pat.is_main, pat.nv);
           }
           catch (WebException ex)
           {
              MessageBox.Show ( ex.Message, "HttpPost: Response error", 
                 MessageBoxButtons.OK, MessageBoxIcon.Error );
              loadi.Hide();
           }
           devolver = "null";
           //ev_paginacarga(devolver,pat.is_main);
        } // end HttpPost 
    }
}
